package com.way.fileexplorer.adapters;

import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.way.fileexplorer.R;
import com.way.fileexplorer.util.FileSort;
import com.way.fileexplorer.util.FileType;

public class MultiSelectFileAdapter extends FileAdapter {

	private String mCurrentFolderPath;
	
	private String mRootFolderPath;

	private boolean showHiddenFile;

	private Stack<String> mFolderStack;

	private static MultiSelectFileAdapter adapter = null;
	
	private Set<String> mCheckedItems=new HashSet<String>();

	private MultiSelectFileAdapter(Context context) {
		super(context);
		mFolderStack = new Stack<String>();
		showHiddenFile = false;
		mIsInit = false;
	}

	public static MultiSelectFileAdapter getMultiSelectFileAdapter(Context context) {
		if (adapter == null) {
			adapter = new MultiSelectFileAdapter(context);
		}else{
			adapter.checkAll(false);
		}
		return adapter;
	}
	
	@Deprecated
	public static String getCurrentFolderPath() {
		return adapter.mCurrentFolderPath;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = super.getView(position, convertView, parent);
		ViewHolder vHolder = (ViewHolder) view.getTag(R.id.action_paste_mode_cancel);
		File file = mFileList.get(position);
		if (file.isDirectory()) {
			vHolder.fileIcon.setImageResource(R.drawable.file_item_folder_ic);
		} else {
			int fileType = FileType.getFileType(mContext).getFileType(file);
			view.setTag(R.id.viewtag_filetype, fileType);
			vHolder.fileIcon.setImageResource(fileType);
		}
		vHolder.selectCb.setVisibility(View.VISIBLE);
		vHolder.selectCb.setChecked(isChecked(position));
		vHolder.fileIcon.setClickable(false);
		
		return view;
	}
	
	public void update() {
		updateFileList(new File(mCurrentFolderPath), false);
	}
	
	public void intoFolder(File folder) {
		updateFileList(folder, true);
	}

	private void updateFileList(File folder, boolean isRecord) {
		if (folder == null) {
			return;
		}
		if (!folder.exists() || !folder.isDirectory()) {
			return;
		}
		File[] allFiles = folder.listFiles();
		if (isRecord) {
			mFolderStack.push(mCurrentFolderPath);
		}
		mCurrentFolderPath = folder.getPath();
		mFileList.clear();
		if (allFiles != null) {
			for (File f : allFiles) {
				if (showHiddenFile || !f.isHidden()) {
				    if(f.exists()){
				        mFileList.add(f);
				    }
				}
			}
			FileSort.fileListSort(mFileList);
		}
	}

	public void callBack() {
		if (mFolderStack.empty()) {
			return;
		}
		File preFile = new File(mFolderStack.pop());
		while(!preFile.exists() && !mFolderStack.isEmpty()) {
			preFile = new File(mFolderStack.pop());
		}
		updateFileList(preFile, false);
	}
	
	public void upBack() {
		if (mRootFolderPath.equals(mCurrentFolderPath)) {
			return;			
		}
		File file = new File(mCurrentFolderPath);
		updateFileList(file.getParentFile(), true);
	}

	public void init(File folder) {
		mFolderStack.clear();
		mCurrentFolderPath = null;
		mRootFolderPath = folder.getPath();
		updateFileList(folder, false);
	}
	
	public boolean isEmptyStack() {
		return mFolderStack.isEmpty();
	}

	public boolean isShowHiddenFile() {
		return showHiddenFile;
	}
	
	public boolean isAtTop() {
		return mRootFolderPath.equals(mCurrentFolderPath);
	}

	public void setShowHiddenFile(boolean showHiddenFile) {
		this.showHiddenFile = showHiddenFile;
	}
	
    public boolean isChecked(int position) {
        return mCheckedItems.contains(getItem(position).toString());
    }
    
    public void setChecked(int position,boolean checked) {
        String file = getItem(position).toString();
        if (checked) {
            mCheckedItems.add(file);
        } else {
            mCheckedItems.remove(file);
        }
    }
    public void checkAll(boolean checked) {
        if (checked) {
            int count = getCount();
            for (int i = 0; i < count; ++i) {
                String file = getItem(i).toString();
                mCheckedItems.add(file);
            }
        } else {
            mCheckedItems.clear();
        }
    }
    
    public boolean isAllChecked(){
        return mCheckedItems.size() == getCount() ? true: false;
    }

}
